package com.base.commons.web.utils;

import com.base.commons.web.bean.ExcelBean;
import jxl.Workbook;
import jxl.write.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

/**
 * Excel生成工具类
 * Created by Irany(欧书剑) 2017/7/28 0028 00:01
 */
public class ExportExcelUtils {
    /**
     * 生成Excel.
     * @param fileName 文件名
     * @param headers 文件标题
     * @param datas 数据
     * @param os 输出流
     */
    public static void createExcel(String fileName, String[] headers, List<? extends ExcelBean> datas, OutputStream os) {
        createExcel(fileName, headers, datas, os, true);
    }

    /**
     * 生成Excel.
     * @param fileName 文件名
     * @param headers 文件标题
     * @param datas 数据
     * @param os 输出流
     * @param imageUseLink 图片显示为链接
     */
    public static void createExcel(String fileName, String[] headers, List<? extends ExcelBean> datas, OutputStream os, boolean imageUseLink) {
        if (datas != null && datas.size() > 0 && os != null && StringUtils.isNotBlank(fileName)) {
            WritableWorkbook workbook = null;

            try {
                workbook = Workbook.createWorkbook(os);
                WritableCellFormat cf = new WritableCellFormat(new WritableFont(WritableFont.createFont("宋体"), 16, WritableFont.BOLD));
                cf.setBorder(Border.ALL, BorderLineStyle.THIN);
                WritableSheet sheet = workbook.createSheet(fileName, 0);

                //创建标题
                if (headers != null && headers.length > 0) {
                    for (int j=0; j < headers.length; j++) {
                        sheet.addCell(new Label(j, 0, headers[j], cf));
                        sheet.setColumnView(j, 25);
                    }
                }

                WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.NO_BOLD);
                cf = new WritableCellFormat(font);
                cf.setBorder(Border.ALL, BorderLineStyle.THIN);

                for (int i=0; i<datas.size(); i++) {
                    ExcelBean data = datas.get(i);
                    List<String> rowData = data.getExcelData();

                    for (int j=0; j < rowData.size(); j++) {
                        String colData = rowData.get(j);

                        if (isAliImage(colData)) {
                            if (imageUseLink) {
                                sheet.addHyperlink(new WritableHyperlink(j, i + 1, new URL(colData)));
                            } else {
                                sheet.addImage(new WritableImage(j, i + 1, 1, 1, IOUtils.toByteArray(new URL(colData)))); // 图片
                            }
                        } else {
                            sheet.addCell(new Label(j, i + 1, colData, cf));
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                if (workbook != null) {
                    try {
                        workbook.write();
                    } catch (IOException e) {}
                    try {
                        workbook.close();
                    } catch (Exception e) {}
                }

                try {
                    os.close();
                } catch (IOException e) {}
            }
        }
    }

    public static boolean isAliImage(String str) {
        if (StringUtils.isNotBlank(str)) {
            if (str.startsWith("http://") && str.contains(".aliyuncs.com")) {
                return true;
            }
        }

        return false;
    }

    private ExportExcelUtils(){}
}
